// API基础配置
const API_BASE_URL = 'http://localhost:5125/api/v1';

// 通用请求函数
async function request<T>(url: string, options: RequestInit = {}): Promise<T> {
  const response = await fetch(`${API_BASE_URL}${url}`, {
    headers: {
      'Content-Type': 'application/json',
      ...options.headers,
    },
    ...options,
  });

  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }

  return response.json();
}

// 用户相关API
export const userApi = {
  // 获取用户列表
  getUsers: (page: number = 1, pageSize: number = 10) =>
    request(`/users?page=${page}&pageSize=${pageSize}`),

  // 获取单个用户
  getUser: (id: number) =>
    request(`/users/${id}`),

  // 创建用户
  createUser: (data: any) =>
    request('/users', {
      method: 'POST',
      body: JSON.stringify(data),
    }),

  // 更新用户
  updateUser: (id: number, data: any) =>
    request(`/users/${id}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    }),

  // 删除用户
  deleteUser: (id: number) =>
    request(`/users/${id}`, {
      method: 'DELETE',
    }),
};

// 商品相关API
export const productApi = {
  // 获取商品列表
  getProducts: (page: number = 1, pageSize: number = 10, category?: string) => {
    const params = new URLSearchParams({
      page: page.toString(),
      pageSize: pageSize.toString(),
    });
    if (category && category !== 'all') {
      params.append('category', category);
    }
    return request(`/products?${params.toString()}`);
  },

  // 获取单个商品
  getProduct: (id: number) =>
    request(`/products/${id}`),

  // 创建商品
  createProduct: (data: any) =>
    request('/products', {
      method: 'POST',
      body: JSON.stringify(data),
    }),

  // 更新商品
  updateProduct: (id: number, data: any) =>
    request(`/products/${id}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    }),

  // 删除商品
  deleteProduct: (id: number) =>
    request(`/products/${id}`, {
      method: 'DELETE',
    }),

  // 获取商品分类
  getCategories: () =>
    request('/products/categories'),
};

// 分类相关API
export const categoryApi = {
  // 获取分类列表
  getCategories: (page: number = 1, pageSize: number = 10, type?: string) => {
    const params = new URLSearchParams({
      page: page.toString(),
      pageSize: pageSize.toString(),
    });
    if (type && type !== 'all') {
      params.append('type', type);
    }
    return request(`/categories?${params.toString()}`);
  },

  // 获取单个分类
  getCategory: (id: number) =>
    request(`/categories/${id}`),

  // 创建分类
  createCategory: (data: any) =>
    request('/categories', {
      method: 'POST',
      body: JSON.stringify(data),
    }),

  // 更新分类
  updateCategory: (id: number, data: any) =>
    request(`/categories/${id}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    }),

  // 删除分类
  deleteCategory: (id: number) =>
    request(`/categories/${id}`, {
      method: 'DELETE',
    }),

  // 获取分类类型
  getTypes: () =>
    request('/categories/types'),
};

// 订单相关API
export const orderApi = {
  // 获取订单列表
  getOrders: (page: number = 1, pageSize: number = 10, status?: string) => {
    const params = new URLSearchParams({
      page: page.toString(),
      pageSize: pageSize.toString(),
    });
    if (status && status !== 'all') {
      params.append('status', status);
    }
    return request(`/orders?${params.toString()}`);
  },

  // 获取单个订单
  getOrder: (id: number) =>
    request(`/orders/${id}`),

  // 创建订单
  createOrder: (data: any) =>
    request('/orders', {
      method: 'POST',
      body: JSON.stringify(data),
    }),

  // 更新订单状态
  updateOrderStatus: (id: number, status: string) =>
    request(`/orders/${id}/status`, {
      method: 'PUT',
      body: JSON.stringify({ status }),
    }),

  // 删除订单
  deleteOrder: (id: number) =>
    request(`/orders/${id}`, {
      method: 'DELETE',
    }),

  // 获取订单状态
  getStatuses: () =>
    request('/orders/statuses'),
};

// 轮播图相关API
export const bannerApi = {
  // 获取轮播图列表
  getBanners: (page: number = 1, pageSize: number = 10, status?: string) => {
    const params = new URLSearchParams({
      page: page.toString(),
      pageSize: pageSize.toString(),
    });
    if (status && status !== 'all') {
      params.append('status', status);
    }
    return request(`/banners?${params.toString()}`);
  },

  // 获取活跃轮播图
  getActiveBanners: () =>
    request('/banners/active'),

  // 获取单个轮播图
  getBanner: (id: number) =>
    request(`/banners/${id}`),

  // 创建轮播图
  createBanner: (data: any) =>
    request('/banners', {
      method: 'POST',
      body: JSON.stringify(data),
    }),

  // 更新轮播图
  updateBanner: (id: number, data: any) =>
    request(`/banners/${id}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    }),

  // 删除轮播图
  deleteBanner: (id: number) =>
    request(`/banners/${id}`, {
      method: 'DELETE',
    }),

  // 获取轮播图状态
  getStatuses: () =>
    request('/banners/statuses'),
};

// AI咨询相关API
export const aiInquiryApi = {
  // 获取AI咨询列表
  getAiInquiries: (page: number = 1, pageSize: number = 10, status?: string) => {
    const params = new URLSearchParams({
      page: page.toString(),
      pageSize: pageSize.toString(),
    });
    if (status && status !== 'all') {
      params.append('status', status);
    }
    return request(`/aiinquiries?${params.toString()}`);
  },

  // 获取单个AI咨询
  getAiInquiry: (id: number) =>
    request(`/aiinquiries/${id}`),

  // 创建AI咨询
  createAiInquiry: (data: any) =>
    request('/aiinquiries', {
      method: 'POST',
      body: JSON.stringify(data),
    }),

  // 更新AI咨询状态
  updateAiInquiryStatus: (id: number, status: string) =>
    request(`/aiinquiries/${id}/status`, {
      method: 'PUT',
      body: JSON.stringify({ status }),
    }),

  // 回复AI咨询
  replyAiInquiry: (id: number, response: string) =>
    request(`/aiinquiries/${id}/reply`, {
      method: 'PUT',
      body: JSON.stringify({ response }),
    }),

  // 删除AI咨询
  deleteAiInquiry: (id: number) =>
    request(`/aiinquiries/${id}`, {
      method: 'DELETE',
    }),

  // 获取AI咨询状态
  getStatuses: () =>
    request('/aiinquiries/statuses'),
}; 